import pygame
import random
import math

# 初始化Pygame
pygame.init()

# 设置窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("足球射门游戏")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)

# 足球和守门员的尺寸
BALL_RADIUS = 20
GOAL_WIDTH = WINDOW_WIDTH // 3
GOAL_HEIGHT = BALL_RADIUS * 2
GOALKEEPER_WIDTH = GOAL_WIDTH // 4
GOALKEEPER_HEIGHT = BALL_RADIUS

# 足球初始位置
ball_x = WINDOW_WIDTH // 2
ball_y = WINDOW_HEIGHT - BALL_RADIUS - 10

# 守门员初始位置
goalkeeper_x = WINDOW_WIDTH // 2 - GOALKEEPER_WIDTH // 2
goalkeeper_y = GOAL_HEIGHT

# 射门力度和方向
shoot_power = 0
shoot_angle = 0
is_shooting = False
mouse_pressed = False

# 游戏状态
game_over = False
result = ""
score = 0
rounds_played = 0
max_rounds = 5

# 重置足球和守门员
def reset_game():
    global ball_x, ball_y, goalkeeper_x, goalkeeper_y, is_shooting, game_over, result
    ball_x = WINDOW_WIDTH // 2
    ball_y = WINDOW_HEIGHT - BALL_RADIUS - 10
    goalkeeper_x = WINDOW_WIDTH // 2 - GOALKEEPER_WIDTH // 2
    goalkeeper_y = GOAL_HEIGHT
    is_shooting = False
    game_over = False
    result = ""

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 鼠标按下事件
        if event.type == pygame.MOUSEBUTTONDOWN:
            if not game_over and math.hypot(event.pos[0] - ball_x, event.pos[1] - ball_y) <= BALL_RADIUS:
                mouse_pressed = True

        # 鼠标抬起事件
        if event.type == pygame.MOUSEBUTTONUP:
            if mouse_pressed and not game_over:
                mouse_pressed = False
                is_shooting = True
                # 计算射门力度和方向
                shoot_power = math.hypot(ball_x - event.pos[0], ball_y - event.pos[1])
                shoot_angle = math.atan2(ball_y - event.pos[1], ball_x - event.pos[0])
                # 守门员随机扑救
                goalkeeper_x += random.randint(-GOALKEEPER_WIDTH, GOALKEEPER_WIDTH)
                goalkeeper_x = max(WINDOW_WIDTH // 2 - GOAL_WIDTH // 2, min(goalkeeper_x, WINDOW_WIDTH // 2 + GOAL_WIDTH // 2 - GOALKEEPER_WIDTH))

        # 按空格键重置游戏
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and game_over and rounds_played < max_rounds:
                reset_game()
                rounds_played += 1

    # 更新足球位置
    if is_shooting and not game_over:
        ball_x += int(shoot_power * math.cos(shoot_angle))
        ball_y += int(shoot_power * math.sin(shoot_angle))
        shoot_power *= 0.98  # 模拟空气阻力

        # 碰撞检测
        if ball_y <= GOAL_HEIGHT:  # 足球进入球门区域
            if goalkeeper_x <= ball_x <= goalkeeper_x + GOALKEEPER_WIDTH:  # 足球碰到守门员
                result = "save"
                game_over = True
            elif WINDOW_WIDTH // 2 - GOAL_WIDTH // 2 <= ball_x <= WINDOW_WIDTH // 2 + GOAL_WIDTH // 2:  # 足球进入球门
                result = "score"
                score += 1
                game_over = True
            else:  # 足球射偏
                result = "lost"
                game_over = True
        elif ball_x < 0 or ball_x > WINDOW_WIDTH or ball_y < 0:  # 足球飞出边界
            result = "lost"
            game_over = True

        # 如果游戏结束，停止足球运动
        if game_over:
            is_shooting = False

    # 清屏
    window.fill(WHITE)

    # 绘制球门
    pygame.draw.rect(window, BLACK, (WINDOW_WIDTH // 2 - GOAL_WIDTH // 2, 0, GOAL_WIDTH, GOAL_HEIGHT))

    # 绘制守门员
    pygame.draw.rect(window, BLUE, (goalkeeper_x, goalkeeper_y, GOALKEEPER_WIDTH, GOALKEEPER_HEIGHT))

    # 绘制足球
    pygame.draw.circle(window, RED, (ball_x, ball_y), BALL_RADIUS)

    # 显示游戏结果和比分
    font = pygame.font.SysFont(None, 55)
    if game_over:
        text = font.render(result, True, BLACK)
        window.blit(text, (WINDOW_WIDTH // 2 - text.get_width() // 2, WINDOW_HEIGHT // 2 - text.get_height() // 2))
    score_text = font.render(f"score: {score}", True, BLACK)
    window.blit(score_text, (10, 10))
    rounds_text = font.render(f"round: {rounds_played}/{max_rounds}", True, BLACK)
    window.blit(rounds_text, (10, 50))

    # 如果五轮结束，显示最终比分
    if rounds_played == max_rounds:
        final_text = font.render(f"game over ! last score: {score}", True, BLACK)
        window.blit(final_text, (WINDOW_WIDTH // 2 - final_text.get_width() // 2, WINDOW_HEIGHT // 2 + 50))

    # 更新显示
    pygame.display.flip()

    # 控制帧率
    pygame.time.Clock().tick(60)

# 退出Pygame
pygame.quit()